package com.rzj.sys_lab_assistant.controller;

import com.rzj.sys_lab_assistant.entity.Student;
import com.rzj.sys_lab_assistant.service.IStudentService;
import com.rzj.sys_lab_assistant.utils.FileNameUtils;
import com.rzj.sys_lab_assistant.utils.FileUtils;
import com.rzj.sys_lab_assistant.utils.ResultsUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import java.util.HashMap;
import java.util.Map;

/**
 * @program: sys_lab_assistant
 * @description:
 * @author: 作者
 * @create: 2021-06-29 19:52
 */
@Controller
@RequestMapping("student")
public class StudentController {

    @Autowired
    IStudentService studentService;

    ResultsUtils resultsUtils;

    @RequestMapping("toAdd")
    public String toAdd() {
        return "student/studentAdd";
    }

    @RequestMapping("toEdit/{studentNo}")
    public String toEdit(Model model, @PathVariable("studentNo") String studentNo) {
        model.addAttribute("student", studentService.getStudentById(studentNo));
        return "student/studentUpdate";
    }

    @RequestMapping("studentList")
    public String studentList(Model model) {
        model.addAttribute("studentList", studentService.listStudents());
        return "student/studentList";
    }

    @RequestMapping("studentAdd")
    @ResponseBody
    public Map<String, String> studentAdd(@RequestParam("studentNo") String studentNo,
                                          @RequestParam("studentName") String studentName,
                                          @RequestParam("studentPwd") String studentPwd,
                                          @RequestParam("studentMajor") String studentMajor,
                                          @RequestParam("studentCollege") String studentCollege,
                                          @RequestParam("studentGrade") String studentGrade,
                                          @RequestParam("studentMobile") String studentMobile,
                                          @RequestParam("studentEmail") String studentEmail,
                                          @RequestParam("imgUrl") String imgUrl) {
        Student student = new Student(studentNo, studentName, studentPwd, studentMajor,
                studentCollege, studentGrade, studentMobile, studentEmail, imgUrl, 1);
        int flag = studentService.saveStudent(student);
        if (flag == 1) {
            return resultsUtils.resultsMap("success", "添加成功！");
        } else {
            return resultsUtils.resultsMap("error", "添加失败，请联系管理员！");
        }
    }

    @RequestMapping("studentEdit")
    @ResponseBody
    public Map<String, String> studentEdit(@RequestParam("studentNo") String studentNo,
                                           @RequestParam("studentName") String studentName,
                                           @RequestParam("studentPwd") String studentPwd,
                                           @RequestParam("studentMajor") String studentMajor,
                                           @RequestParam("studentCollege") String studentCollege,
                                           @RequestParam("studentGrade") String studentGrade,
                                           @RequestParam("studentMobile") String studentMobile,
                                           @RequestParam("studentEmail") String studentEmail,
                                           @RequestParam("imgUrl") String imgUrl) {
        Student student = new Student(studentNo, studentName, studentPwd, studentMajor,
                studentCollege, studentGrade, studentMobile, studentEmail, imgUrl, 1);
        int flag = studentService.updateStudent(student);
        if (flag == 1) {
            return resultsUtils.resultsMap("success", "修改成功！");
        } else {
            return resultsUtils.resultsMap("error", "修改失败，请联系管理员！");
        }
    }

    @RequestMapping("studentDel")
    @ResponseBody
    public Map<String, String> studentDel(@RequestParam("studentNo") String studentNo) {
        int flag = studentService.deleteStudent(studentNo);
        if (flag == 1) {
            return resultsUtils.resultsMap("success", "删除成功！");
        } else {
            return resultsUtils.resultsMap("error", "删除失败，请联系管理员！");
        }
    }

    /**
     * @param file 要上传的文件
     * @return
     */
    @RequestMapping("fileUpload")
    @ResponseBody
    public Map<String, String> upload(@RequestParam(value = "file") MultipartFile file) {

        Map<String, String> map = new HashMap<>();
        // 要上传的目标文件存放路径
        String localPath = "E:/Code_workerpalce/sys_lab_assistant/src/main/resources/static/img";
        String sqlPath = "http://localhost:9527/sysLab/img/";
        // 上传成功或者失败的提示
        String msg = "";
        String data = "";

        //System.out.println("file==>" + file.getOriginalFilename());

        String fileName = FileNameUtils.getFileName(file.getOriginalFilename());
        String fileUrl = sqlPath + fileName;
        //System.out.println("fileUrl==>" + fileUrl);

        //System.out.println("fileName==>" + fileName);

        if (FileUtils.upload(file, localPath, fileName)) {
            // 上传成功，给出页面提示
            data = "success";
            msg = "上传成功！";
            map.put("fileUrl", fileUrl);
        } else {
            data = "error";
            msg = "上传失败！";

        }

        // 显示图片
        map.put("data", data);
        map.put("msg", msg);

        //map.put("fileName", file.getOriginalFilename());

        return map;
    }




}
